# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "Header.h"
# include "DirectoryOps.h"
# include "NArrayTree.h"

char **listDir(char *DirName,int flag, struct NArray_node *NArrayRoot) { 
	struct NArray_node *temp;
	int i = 0, l = 0;
	char **list = NULL;
	temp = searchNArrayNode(NArrayRoot, DirName);
	if(temp != NULL) {
		list = (char **) malloc (100 * sizeof(char *));
		if(flag == 0) {
			if( temp -> leftChild) {
				temp = temp -> leftChild;
				while(temp) {
					*(list + i) = (char *) malloc (100 * sizeof(char));
					copyString (*(list + i), temp -> fd -> fileName);
					temp = temp -> rightSibling;
					i++;
				}	
			}
			*(list + i) = (char *) malloc (100 * sizeof(char));
			copyString (*(list + i), " ");
		}
		else {
			listSubDir(temp -> leftChild, list, &l);
			*(list + l) = (char *) malloc (100 * sizeof(char));
			copyString (*(list + l), " ");
		}
	}
	return list;
}	

void listSubDir (struct NArray_node *root, char **list, int *line) {
	if (root == NULL)
		return;
	*(list + *line) = (char *) malloc (100 * sizeof(char));
	copyString (*(list + *line), root -> fd -> fileName);
	(*line)++;
	listSubDir (root -> leftChild, list, line);
	listSubDir (root -> rightSibling, list, line);
}

